//
//  ChooseAlbumViewController.m
//  iOS-CtrlV
//
//  Created by 龚 杰 on 12-12-28.
//  Copyright (c) 2012年 Wang Li. All rights reserved.
//

#import "ChooseAlbumViewController.h"
#import "MainTabBarController.h"
#import <QuartzCore/QuartzCore.h>
#import "AlbumModel.h"

@interface ChooseAlbumViewController ()

@end

@implementation ChooseAlbumViewController
@synthesize delegate;
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}
- (id)init
{
    if (self = [super init]) {
        m_AlbumListdata =[[NSMutableArray alloc] init];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    
    // Do any additional setup after loading the view.
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
	// Do any additional setup after loading the view.
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        [self.navigationController.navigationBar setBackgroundImage:tAppDel.m_MidNavImg forBarMetrics: UIBarMetricsDefault];
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    m_BackBttBarItem =[UIButton buttonWithType:UIButtonTypeCustom];
    m_BackBttBarItem.frame =CGRectMake(0, 0, 44, 44);
    [m_BackBttBarItem setImage:[UIImage imageNamed:@"navclose.png"] forState:UIControlStateNormal];
    [m_BackBttBarItem addTarget:self action:@selector(navBackBtt) forControlEvents:UIControlEventTouchUpInside];
    [m_BackBttBarItem retain];
    [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    
    
    
    m_SearchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];
    [m_SearchView setBackgroundColor:[UIColor colorWithRed:236/255.f green:238/255.f blue:235/255.f alpha:1]];
    
    CALayer* tSearchLayer = [CALayer layer];
    tSearchLayer.borderWidth =1;
    tSearchLayer.borderColor = [UIColor colorWithRed:193/255.f green:193/255.f blue:193/255.f alpha:1].CGColor;
    [tSearchLayer setBackgroundColor:[UIColor whiteColor].CGColor];
    tSearchLayer.frame =CGRectMake(8, 12, 240, 30);
    [m_SearchView.layer addSublayer:tSearchLayer];
    UITextField* tSearchField =[[UITextField alloc] initWithFrame:CGRectMake(12, 17, 235, 30)];
    tSearchField.placeholder =@"输入创建专辑名称";
    tSearchField.delegate = self;
    tSearchField.tag =200;
    [tSearchField setFont:[UIFont systemFontOfSize:16]];
    [m_SearchView addSubview:tSearchField];
    tSearchField.returnKeyType = UIReturnKeyDone;
    [tSearchField release];
    
    UIButton* tCancelBtt = [UIButton buttonWithType:UIButtonTypeCustom];
    [tCancelBtt.titleLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"searchbttbg.png"]]];
    [tCancelBtt setTitleColor:[UIColor colorWithRed:94/255.f green:94/255.f blue:94/255.f alpha:1] forState:UIControlStateNormal];
    tCancelBtt.layer.borderColor =[UIColor colorWithRed:193/255.f green:193/255.f blue:193/255.f alpha:1].CGColor;
    tCancelBtt.layer.borderWidth =1;
    tCancelBtt.layer.cornerRadius =6.f;
    [tCancelBtt addTarget:self action:@selector(CreateAlbum) forControlEvents:UIControlEventTouchUpInside];
    [tCancelBtt setTitle:@"创建" forState:UIControlStateNormal];
    tCancelBtt.frame =CGRectMake(256, 12, 58, 30);
    [m_SearchView addSubview:tCancelBtt];
    
    m_Tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49) style:UITableViewStylePlain];
    [m_Tableview setDataSource:self];
    [m_Tableview setDelegate:self];
    m_Tableview.tableHeaderView = m_SearchView;
    [self.view addSubview:m_Tableview];
    

    NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
    m_myuserid = [tUserDef stringForKey:@"user_id"];
    if (m_myuserid.length>0) {
        [[MyHttpProcessor sharedManager] RequestAlbumSearchListGet:m_myuserid];
        [MyHttpProcessor sharedManager].delegate = self;
        [self addHudLoad];
    }else {

    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [m_BackBttBarItem removeFromSuperview];

}
- (void)viewWillAppear:(BOOL)animated
{
    if ([m_BackBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    }
    MainTabBarController* tMainTabBar = (MainTabBarController*)self.navigationController.tabBarController;
    [tMainTabBar.m_TabbarCustom removeFromSuperview];
    [self.view addSubview:tMainTabBar.m_TabbarCustom];
}

- (void)navBackBtt
{
    MainTabBarController* tMainTabBar = (MainTabBarController*)self.navigationController.tabBarController;
    tMainTabBar.m_TabbarCustom.hidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)CreateAlbum
{
   UITextField* ifield = (UITextField*)[m_SearchView viewWithTag:200];
   [ifield resignFirstResponder];
    m_createname = ifield.text;
    NSLog(@"ifield  = %@",m_createname);
    if (![m_createname isEqual:@""]&&m_createname.length>0) {
        [[MyHttpProcessor sharedManager] RequestAlbumCreatePost:m_myuserid title:m_createname];
        [MyHttpProcessor sharedManager].delegate = self;
        [self addHudLoad];
    }else{
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [tAppDel showWithCustomView:@"提示" detailText:@"请输入专题名" isCue:NO delayTime:2.0f];
    }

}
#pragma mark-
#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (delegate != nil) {
        [delegate getAlbumInformation:[m_AlbumListdata objectAtIndex:indexPath.row]];
        [self navBackBtt];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else if(section == 1){
        return 60;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}
#pragma mark-
#pragma UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_AlbumListdata count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    NSInteger row = indexPath.row;
    static NSString* tSStrCell = @"logincell";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:tSStrCell];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tSStrCell] autorelease];
       UILabel* iAlbumbel = [[UILabel alloc] initWithFrame:CGRectMake(15, 13, tCell.frame.size.width-30, 20)];
        iAlbumbel.tag = 100;
        iAlbumbel.backgroundColor = [UIColor clearColor];
        iAlbumbel.font = [UIFont boldSystemFontOfSize:18.0f];
        iAlbumbel.textColor = [UIColor blackColor];
        [tCell.contentView addSubview:iAlbumbel];
        [iAlbumbel release];
    }
    
        AlbumModel* amodel = [m_AlbumListdata objectAtIndex:row];
        UILabel* title = (UILabel*)[tCell viewWithTag:100];
        title.text = amodel.albumtitle;
    
    return tCell;
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"text1 = %@",textField.text);
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"text2 = %@",textField.text);
    return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    NSLog(@"text3 = %@",textField.text);
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)OutAlbumSearchListData:(NSMutableArray*)_data
{
    [self hudWasHidden:nil];
    if ([_data count] == 0) {
        return;
    }
    for(int i = 0; i < [_data count]; ++i) {
        [m_AlbumListdata addObject:[_data objectAtIndex:i]];
    }
    [m_Tableview reloadData];
}
- (void)OutAlbumCreate:(AlbumModel*)_data
{
    [self hudWasHidden:nil];
    if (_data!=nil) {
        [m_AlbumListdata insertObject:_data atIndex:0];
        [m_Tableview reloadData];
    }
}
- (void)OurErrer:(NSDictionary*)dic
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"errorMsg"] isCue:YES delayTime:3.0f];
    [self hudWasHidden:nil];
}
#pragma mark -
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    m_MBHUD.delegate = nil;
	[m_MBHUD removeFromSuperview];
	[m_MBHUD release];
	
	m_MBHUD=nil;
}

-(void) addHudLoad{
	if (m_MBHUD == nil) {
		m_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
		[self.navigationController.view addSubview:m_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		m_MBHUD.delegate = self;
		m_MBHUD.labelText = @"请求数据中...";
		[m_MBHUD show:YES];
	}
}

- (void)dealloc
{
    [m_SearchView release];
    [m_Tableview release];
    
    m_BackBttBarItem = nil;
    m_MBHUD = nil;
    [m_AlbumListdata release];
    m_myuserid = nil;
    m_createname = nil;

    [super dealloc];
}
@end
